package com.xyu.gulimall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.xyu.gulimall.product.service.CategoryBrandRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xyu.common.utils.PageUtils;
import com.xyu.common.utils.Query;

import com.xyu.gulimall.product.dao.BrandDao;
import com.xyu.gulimall.product.entity.BrandEntity;
import com.xyu.gulimall.product.service.BrandService;
import org.springframework.util.StringUtils;

//品牌表
@Service("brandService")
public class BrandServiceImpl extends ServiceImpl<BrandDao, BrandEntity> implements BrandService {

    @Autowired
    private CategoryBrandRelationService categoryBrandRelationService;
    @Override
    //商品关联表分页查询
    public PageUtils queryPage(Map<String, Object> params) {
        //获取查询条件
        String key = (String) params.get("key");
        //创建条件构造器
        QueryWrapper<BrandEntity> wrapper = new QueryWrapper<>();
        //如果由查询条件则进行构造
        if(!StringUtils.isEmpty(key)){
            wrapper.eq("brand_id",key).or().like("name",key);
        }
        //根据分则，查询条件执行查询
        IPage<BrandEntity> page = this.page(new Query<BrandEntity>().getPage(params),wrapper);
        return new PageUtils(page);
    }

    @Override
    public void updateDetail(BrandEntity brand) {
        //更新自己保证冗余数据的一致性
        this.updateById(brand);
            //同步更新其他数据
            categoryBrandRelationService.updateBrand(brand.getBrandId(),brand.getName());
            //TODO 更新其他关联

    }

}
